package com.anytron.service.impl;

import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.anytron.base.service.impl.BaseServiceImpl;
import com.anytron.mapper.TimerLogsMapper;
import com.anytron.model.TimerLogs;
import com.anytron.model.req.PageRequest;
import com.anytron.service.TimerLogsService;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;

@Service
public class TimerLogsServiceImpl extends BaseServiceImpl<TimerLogs> implements TimerLogsService {

	@Autowired
	public void setMapper(TimerLogsMapper mapper) {
		this.mapper = mapper;
	}

	@Override
	public PageInfo<TimerLogs> queryDataByPage(PageRequest pageRequest, TimerLogs timerLogs) {
		Example example = new Example(TimerLogs.class);
		Example.Criteria criteria = example.createCriteria();
		System.out.println(timerLogs.getTimerTimeStr());
		example.setOrderByClause("timer_time desc");
		if (StringUtils.isNotBlank(timerLogs.getAppName())) {
			criteria.andLike("appName", "%" + timerLogs.getAppName() + "%");
		}
		if (StringUtils.isNotBlank(timerLogs.getConfigName())) {
			criteria.andLike("configName", "%" + timerLogs.getConfigName() + "%");
		}
		if (StringUtils.isNoneBlank(timerLogs.getTimerTimeStr())) {
			try {
				Date startDate = DateUtils.parseDate(timerLogs.getTimerTimeStr(), "yyyy-MM-dd");
				Date endDate = DateUtils.addDays(startDate, 1);
				criteria.andBetween("timerTime", startDate, endDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		PageInfo<TimerLogs> pageInfo = pageByExample(pageRequest, example);
		return pageInfo;
	}

}
